package com.nchu.react.webflux.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nchu.react.webflux.model.Student;
import com.nchu.react.webflux.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author fujianjian
 * @project react-learning
 * @date 2018/9/21 下午2:22
 */
@Service
public class StudentServiceImpl implements StudentService {

    private Map<Integer, Student> TEMP_STUDENT_CONTAINER = Maps.newHashMap();


    public StudentServiceImpl() {

        Student.StudentBuilder studentBuilder = Student.builder();
        for (int i = 0; i < 10.; i++) {
            Student student = studentBuilder.id(i).address("金沙江路2890弄").admissionTime(new Date()).age(10).clazz("三年二班")
                    .name("Allen" + i).build();
            TEMP_STUDENT_CONTAINER.put(student.getId(), student);

        }
    }

    @Override
    public Student createUser(Student student) {
        this.TEMP_STUDENT_CONTAINER.put(student.getId(), student);
        return student;
    }

    @Override
    public Student getById(Integer id) {

        return TEMP_STUDENT_CONTAINER.get(id);
    }

    @Override
    public List<Student> fetchAllStudent() {
        List<Student> list = Lists.newArrayList();
        TEMP_STUDENT_CONTAINER.forEach((k,v) -> list.add(v));
        return list;
    }
}
